<?php

	/*
	
	This is used to display images, pass in the image and size and 
	
	it will hook into the thumbnail script to display the image.
	
	*/

	function SPImage($image="",$size="s"){

		$ext="";
 

		if($image ==""){

		return get_option("imagestorage_link")."noimg.gif";

		}
	
		switch($size){
		
			case "s": { $ext ="&s=small"; } break;
			case "m": { $ext ="&s=medium"; } break;
			case "l": { $ext ="&s=large"; } break;
			default: { 	$ext ="&s=small"; } break;
		
		}

		if(substr($image, - 9) =="noimg.gif"){
		return get_option("imagestorage_link")."noimg.gif";
		}
 
		// IS THIS A LINKED IMAGE OR STORED IMAGE?
		$pos = stripos($image, "http");
		if ($pos !== false) {
			return $image;
		}else{
			if(get_option("shop_image_cache") =="yes" && $size != "f" ){
			return get_option("imagestorage_link")."sp.php?file=".$image.$ext;
			}else{
			return get_option("imagestorage_link").$image;
			}
		}	
	}

	/*
	
	This is used to get the image from its current 
	
	location online and download/store it on the server
	
	*/
	
	function SaveImage($FileName,$ext=""){
 
		global $wpdb;

		$STORE_PATH = get_option("imagestorage_path");

		if($STORE_PATH ==""){
			return "";
		}

		if($FileName ==""){
			return "";
		}
	
		$target = $FileName;
		$ch = curl_init ($target);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
		$rawdata=curl_exec ($ch);
		curl_close ($ch);
		
		$ff = explode("/",$FileName);
		$tt = count($ff)-1;		 
		
		$FileName = $ff[$tt];
		$fp = fopen($STORE_PATH.$ext.$FileName,'w');
		fwrite($fp, $rawdata);
		fclose($fp); 
	
		return $ext.$FileName;
	}



	/*
	
	This is used to get the MIME 
	
	type for the file being added.
	
	*/

    function returnMIMEType($ext)
    {
 
        switch(strtolower($ext))
        {
            case "js" :
                return "application/x-javascript";

            case "json" :
                return "application/json";

            case "jpg" :
            case "jpeg" :
            case "jpe" :
                return "image/jpg";

            case "png" :
            case "gif" :
            case "bmp" :
            case "tiff" :
                return "image/".strtolower($fileSuffix[1]);

            case "css" :
                return "text/css";

            case "xml" :
                return "application/xml";

            case "doc" :
            case "docx" :
                return "application/msword";

            case "xls" :
            case "xlt" :
            case "xlm" :
            case "xld" :
            case "xla" :
            case "xlc" :
            case "xlw" :
            case "xll" :
                return "application/vnd.ms-excel";

            case "ppt" :
            case "pps" :
                return "application/vnd.ms-powerpoint";

            case "rtf" :
                return "application/rtf";

            case "pdf" :
                return "application/pdf";

            case "html" :
            case "htm" :
            case "php" :
                return "text/html";

            case "txt" :
                return "text/plain";

            case "mpeg" :
            case "mpg" :
            case "mpe" :
                return "video/mpeg";

            case "mp3" :
                return "audio/mpeg3";

            case "wav" :
                return "audio/wav";

            case "aiff" :
            case "aif" :
                return "audio/aiff";

            case "avi" :
                return "video/msvideo";

            case "wmv" :
                return "video/x-ms-wmv";

            case "mov" :
                return "video/quicktime";

            case "zip" :
                return "application/zip";

            case "tar" :
                return "application/x-tar";

            case "swf" :
                return "application/x-shockwave-flash";

            default :
            return "application/octet-stream";
        }
    }



	function shopperPress_lightbox ($images) {
	  $matches = explode(",", $images);
		foreach($matches as $var) {
	
			if ($var != "") {
				 
				echo "<a href=\"".SPImage($var,"s")."\" rel=\"lightbox[album]\"><img src=\"".SPImage($var,"s")."\" class=\"size-thumbnail\" alt=\"".get_the_title()."\" title=\"".get_the_title()."\"  style='padding-bottom:15px; border:0px;' /></a>"."\n";
			
			} else {
	
				if ( $matches[0] == "") {
					 
				}
	
			}
		}
	}

?>